<%@page import="com.ddd.entity.Journal"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="com.ddd.entity.Article"%>
<%@page import="com.ddd.service.ArticleService"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.JournalService"%>
<%@page import="com.ddd.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    JournalService journalService = new JournalService(request);
    ArrayList journalList = journalService.loadAll();

    ArticleService articleService = new ArticleService(request);

    String mode = request.getParameter("mode");
    String tjcdCode = request.getParameter("tjcdCode");
    Article article = new Article();

    if (Util.isBlank(mode)) {
        mode = "add";
    } else if (!mode.equals("add")) {
        article = articleService.load(tjcdCode);
    }

    String message = request.getParameter("message");
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            $(function() {
            });
            
            function doUpdate (){
                //$( "#dialog:ui-dialog" ).dialog( "destroy" );
                $( "#dialog_update" ).dialog({
                    title: "更新文章",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "更新": function() {
                            $( this ).dialog( "close" );
                            $('#form1').submit();
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
             
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <!-- starrt #content -->
                        <div id="content">
                            <form id="form1" name="form1" action="../doArticle" method="post">
                                <input type="hidden" id="mode" name="mode" value="<%=mode%>"></input>
                                <input type="hidden" id="cid" name="cid" value="<%=article.getCid()%>"></input>
                                <input type="hidden" id="ctime" name="ctime" value="<%=article.getCtime()%>"></input>
                                <input type="hidden" id="uid" name="uid" value="<%=article.getUid()%>"></input>
                                <input type="hidden" id="utime" name="utime" value="<%=article.getUtime()%>"></input>
                                <table>
                                    <tr>
                                        <td align="left">TJCD碼</td>
                                        <%if (mode.equals("update")) {%>
                                        <td><input id="tjcdCode" name="tjcdCode" value="<%=article.getTjcdCode()%>" readonly></td>
                                            <%} else {%>
                                        <td><input id="tjcdCode" name="tjcdCode" value="<%=article.getTjcdCode()%>"></td>
                                            <%}%>
                                    </tr>
                                    <tr>
                                        <td align="left">期刊名稱</td>
                                        <td>
                                            <select id="fkJournalID" name="fkJournalID">
                                                <% for (int i = 0; i < journalList.size(); i++) {
                                                        Journal j = (Journal) journalList.get(i);
                                                        if (article.getFkJournalId().equals(j.getId())) {
                                                %>
                                                <option value="<%=j.getId()%>" selected="<%=j.getName()%>"><%=j.getName()%></option>
                                                <%} else {%>
                                                <option value="<%=j.getId()%>"><%=j.getName()%></option>
                                                <%}
                                                    }%>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="left">篇名</td>
                                        <td><input id="name" name="name" value="<%=article.getName()%>"></td>
                                    </tr>

                                    <tr>
                                        <td>作者</td>
                                        <td><input type="text" id="author" name="author" value="<%=article.getAuthor()%>"></td>
                                    </tr>

                                    <tr>
                                        <td>卷</td>
                                        <td><input type="text" id="volume" name="volume" value="<%=article.getVolume()%>"></td>
                                    </tr>

                                    <tr>
                                        <td>期</td>
                                        <td><input type="text" id="issue" name="issue" value="<%=article.getIssue()%>"></td>
                                    </tr>

                                    <tr>
                                        <td>出版年</td>
                                        <td><input type="text" id="publishYear" name="publishYear" size="4" value="<%=article.getPublishYear()%>"></td>
                                    </tr>

                                    <tr>
                                        <td align="left">授權狀況</td>
                                        <%if (Util.isNotBlank(article.getAuthorized()) && article.getAuthorized().equals("on")) {%>
                                        <td><input id="authorized" name="authorized" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="authorized" name="authorized" type="checkbox"></td>
                                            <%}%>
                                    </tr>

                                    <tr>
                                        <td align="left">授權書</td>
                                        <%if (Util.isNotBlank(article.getAuthorizationLetter()) && article.getAuthorizationLetter().equals("on")) {%>
                                        <td><input id="authorizationLetter" name="authorizationLetter" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="authorizationLetter" name="authorizationLetter" type="checkbox"></td>
                                            <%}%>
                                    </tr>

                                    <tr>
                                        <td align="left">是否為數位檔</td>
                                        <%if (Util.isNotBlank(article.getDigitalFormat()) && article.getDigitalFormat().equals("on")) {%>
                                        <td><input id="digitalFormat" name="digitalFormat" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="digitalFormat" name="digitalFormat" type="checkbox"></td>
                                            <%}%>
                                    </tr>

                                    <tr>
                                        <td align="left">現/過刊</td>
                                        <%if (Util.isNotBlank(article.getCurrJournal()) && article.getCurrJournal().equals("on")) {%>
                                        <td><input id="currJournal" name="currJournal" type="checkbox" checked></td>
                                            <%} else {%>
                                        <td><input id="currJournal" name="currJournal" type="checkbox"></td>
                                            <%}%>
                                    </tr>

                                    <tr>
                                        <td align="left">備註</td>
                                        <td><textarea id="note" name="note" cols="30" rows="5"><%=article.getNote()%></textarea></td>
                                    </tr>
                                    <tr>
                                        <%if (mode.equals("update")) {%>
                                        <td colspan="2" align="right"> 
                                            <input type="button" value="更新" onclick="doUpdate();" />
                                        </td>
                                        <%} else {%>
                                        <td colspan="2" align="right"> 
                                            <input type="submit" value="新增"/>
                                        </td>
                                            <%}%>
                                    </tr>
                                </table>

                            </form>
                            <div style="clear: both;">&nbsp;</div>
                        </div>
                        <!-- end #content -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_update" style="display: none">
            <p>是否要更新文章？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>